import AppHeader from "../../../components/console/app/appHeader";
import {Content} from "antd/es/layout/layout";
import AppMenu from "../../../components/console/app/appMenu";
import AppBreadCrumb from "../../../components/console/app/appBreadcrumb";
import AppContent from "../../../components/console/app/appContent";
import AppFooter from "../../../components/console/app/appFooter";
import {useEffect, useState} from "react";
import ConsoleRoutes from "../../../routes/consoleRoutes";
import {Layout, message} from "antd";
import {matchPath, useNavigate} from "react-router-dom";
import TokenUtils from "../../../utils/tokenUtils";

/**
 * 控制台主页面, 内部根据路由加载不同的组件
 *
 * @param props
 * @returns {JSX.Element}
 * @constructor
 */
const ConsoleHomePage = (props) => {

    const navigate = useNavigate();
    // location对象
    const location = props.location;

    // 提示框组件
    const [messageApi, contextHolder] = message.useMessage();

    // 当前面包屑
    const [currentBreadcrumb, setCurrentBreadcrumb] = useState("首页");

    /**
     * 递归查找匹配的路由（支持多层children）
     * @param {Array} routes 路由数组
     * @param {string} path 当前路径
     * @returns 匹配的路由对象
     */
    const findMatchedRoute = (routes, path) => {
        for (const route of routes) {
            // 先检查当前路由是否匹配
            if (matchPath(route.path, path) !== null) {
                return route;
            }
            // 如果有children，递归查找子路由
            if (route.children && route.children.length > 0) {
                const matchedChild = findMatchedRoute(route.children, path);
                if (matchedChild) {
                    return matchedChild;
                }
            }
        }
        return null;
    };

    /**
     * 监听页面path地址变化, 及时更新面包屑
     */
    useEffect(() => {
        // 还没有进到控制台的页面不受影响
        if (location.pathname.indexOf("/console") === -1) {
            return;
        }

        // 使用递归函数查找匹配的路由（包括子路由）
        const matchedRoute = findMatchedRoute(ConsoleRoutes, location.pathname);

        // 判断当前用户是否有访问该页面的权限
        let currentPagePermissionCodes = TokenUtils.getCurrentPagePermissionCodes() || [];
        if (!currentPagePermissionCodes.includes(matchedRoute.pageCode)) {
            messageApi.error("当前用户没有访问该页面的权限, 即将退出当前页面")
                .then(() => {
                    if (location.pathname === "/console/index") {
                        navigate("/login");
                    } else {
                        if (currentPagePermissionCodes.includes("index")) {
                            navigate("/console/index");
                        } else {
                            navigate("/login");
                        }
                    }
                })
        }

        // 修改面包屑
        if (matchedRoute) {
            setCurrentBreadcrumb(matchedRoute.breadcrumb);
        }
        // eslint-disable-next-line
    }, [location.pathname]);

    return (
        <Layout style={{
            height: "100vh",
            backgroundColor: "#f0f2f5",
            overflow: "hidden"
        }}>
            {contextHolder}

            {/* 网页头部 */}
            <AppHeader/>

            {/* 网页主要部分 */}
            <Content style={{
                height: "calc(100vh - 114px)",
                margin: 0,
                padding: 0,
                overflow: "hidden"
            }}>
                <Layout>
                    {/* 菜单部分 */}
                    <AppMenu
                        location={location}
                    />

                    {/* 网页主要内容部分 */}
                    <Layout.Content
                        style={{
                            height: "calc(100vh - 114px)",
                            padding: "20px",
                            background: "#f0f2f5",
                            overflow: "auto"
                        }}
                    >
                        {/* 面包屑 */}
                        <AppBreadCrumb
                            currentBreadcrumb={currentBreadcrumb}
                        />

                        {/* 路由内容容器 */}
                        <AppContent/>

                    </Layout.Content>
                </Layout>
            </Content>

            {/* 网页底部 */}
            <AppFooter/>
        </Layout>
    )
}

export default ConsoleHomePage;